package com.bnz.service.impl;

import com.bnz.entity.Classes;
import com.bnz.entity.PageResult;
import com.bnz.entity.Student;
import com.bnz.entity.StudentVo;
import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;
    //1. 列表学生
    @Override
    public List<Student> findAll() {
        //1.1 查询所有学生
        List<Student> students = studentMapper.selectByExample(null);
        //1.2 绑定学生与班级名称关系
        for (Student student : students) {
            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
            student.setCname(classes.getCname());
        }
        //1.3 返回
        return students;
    }

    //2. 分页查询
    @Override
    public PageResult<Student> findByPage(Integer page, Integer pageSiz) {
        //2.1 开始分页
        PageHelper.startPage(page,pageSiz);
        //2.2 进行查询
        //2.2.1 开始实例查询
        Example example = new Example(Student.class);
        List<Student> students = studentMapper.selectByExample(example);
        //2.2.2 遍历学生集合并与班级名称绑定
        for (Student student : students) {
            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
            student.setCname(classes.getCname());
        }
        //2.3 将上面的学生集合转换为page对象
        Page<Student> studentPage = (Page<Student>) students;
        //2.4 返回pageResult
        return new PageResult<>(studentPage.getTotal(),
                studentPage.getPages(),
                page,
                studentPage.getResult());
    }

    //3. 条件查询带分页
    @Override
    public PageResult<Student> search(Integer page, Integer pageSize, StudentVo vo) {
        //3.1 开始分页
        PageHelper.startPage(page,pageSize);
        //3.2 进行查询
        //3.2.1 开始实例查询
        Example example = new Example(Student.class);
        Example.Criteria criteria = example.createCriteria();
        if (vo != null){
            if (!StringUtils.isEmpty(vo.getSname())){
                criteria.andLike("sname","%" + vo.getSname() + "%");
            }
            if (!StringUtils.isEmpty(vo.getAddr())){
                criteria.andLike("addr","%" + vo.getAddr() + "%");
            }
            if (vo.getCid() != null && vo.getCid() != 0){
                criteria.andEqualTo("cid",vo.getCid());
            }
        }
        //3.2.2 封装查询条件
        List<Student> students = studentMapper.selectByExample(example);
        //3.2.3 遍历学生集合并将班级名称与学生绑定
        for (Student student : students) {
            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
            student.setCname(classes.getCname());
        }
        //3.3 将上面的学生集合转换为page对象
        Page<Student> studentPage = (Page<Student>) students;
        //3.4 返回
        return new PageResult<>(studentPage.getTotal(),
                studentPage.getPages(),
                page,
                studentPage.getResult());
    }

    //4. 添加学生
    @Override
    public void add(Student student) {
        studentMapper.insert(student);
    }

    //5. 修改学生
    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }

    //6. 删除学生
    @Override
    public void delete(int id) {
        studentMapper.deleteByPrimaryKey(id);
    }

}
